package com.meb.imageexif

import android.os.Bundle
import android.text.Editable
import com.google.gson.Gson
import com.hjq.toast.ToastUtils
import com.meb.imageexif.entity.ExifTemplateEntity
import com.sugar.sugarlibrary.base.BaseActivity
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog
import com.wdullaer.materialdatetimepicker.time.TimePickerDialog
import kotlinx.android.synthetic.main.activity_template.*
import java.util.*

class TemplateActivity: BaseActivity() {
    override fun init(savedInstanceState: Bundle?) {
        flat_save_action.setOnClickListener {
            saveTemplate()
        }

        et_gps_date.setOnClickListener {
            val calendar = Calendar.getInstance()

            val datePicker = DatePickerDialog.newInstance(
                { view, year, monthOfYear, dayOfMonth ->
                    val month = monthOfYear + 1
                    var monthSt = month.toString()
                    if (month < 10){
                        monthSt = "0$month"
                    }
                    var daySt = dayOfMonth.toString()
                    if (dayOfMonth < 10){
                        daySt = "0$dayOfMonth"
                    }
                    val timeYear = "$year:$monthSt:$daySt"
                    et_gps_date.text = timeYear
                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)
            )
            datePicker.show(supportFragmentManager, "et_gps_date")
        }
        et_gps_time.setOnClickListener {
            val calendar = Calendar.getInstance()
            TimePickerDialog.newInstance(
                { view, hourOfDay, minute, second ->
                    var hday = hourOfDay.toString()
                    if (hourOfDay < 10){
                        hday = "0$hourOfDay"
                    }
                    var minS = minute.toString()
                    if (minute < 10){
                        minS = "0$minute"
                    }
                    var secondS = second.toString()
                    if (second < 10){
                        secondS = "0$second"
                    }
                    et_gps_time.text = "$hday:$minS:$secondS" },
                calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE),
                calendar.get(Calendar.SECOND),
                true
            ).show(supportFragmentManager, "et_gps_time")
        }

        et_date.setOnClickListener {
            val calendar = Calendar.getInstance()

            val datePicker = DatePickerDialog.newInstance(
                { view, year, monthOfYear, dayOfMonth ->
                    val month = monthOfYear + 1
                    var monthSt = month.toString()
                    if (month < 10){
                        monthSt = "0$month"
                    }
                    var daySt = dayOfMonth.toString()
                    if (dayOfMonth < 10){
                        daySt = "0$dayOfMonth"
                    }
                    val timeYear = "$year:$monthSt:$daySt"
                    showTimeDate(calendar, timeYear)
                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)
            )
            datePicker.show(supportFragmentManager, "et_date")
        }
    }

    private fun showTimeDate(calendar: Calendar, timeYear: String) {
        TimePickerDialog.newInstance(
            { view, hourOfDay, minute, second ->
                var hday = hourOfDay.toString()
                if (hourOfDay < 10){
                    hday = "0$hourOfDay"
                }
                var minS = minute.toString()
                if (minute < 10){
                    minS = "0$minute"
                }
                var secondS = second.toString()
                if (second < 10){
                    secondS = "0$second"
                }
                val timeShow = "$timeYear $hday:$minS:$secondS"
                et_date.text = timeShow
            },
            calendar.get(Calendar.HOUR_OF_DAY),
            calendar.get(Calendar.MINUTE),
            calendar.get(Calendar.SECOND),
            true
        ).show(supportFragmentManager, "et_date")
    }

    override fun loadData() {
        loadTemplate()
    }

    override fun getContentView(): Int {
       return R.layout.activity_template
    }

    private fun loadTemplate() {
        val exifEntityJson = mainKv.decodeString(JSON_EXIF, "")
        if (exifEntityJson.isNotEmpty()){
            val entity = Gson().fromJson<ExifTemplateEntity>(exifEntityJson, ExifTemplateEntity::class.java)
            if (entity != null){
                et_make.text = Editable.Factory.getInstance().newEditable(entity.make)
                et_mode.text = Editable.Factory.getInstance().newEditable(entity.mode)
                val gpsDate = entity.gpsDate
                if (gpsDate != null &&gpsDate.isNotEmpty()) {
                    et_gps_date.text = gpsDate
                }
                val gpsTime = entity.time
                if (gpsTime != null && gpsTime.isNotEmpty()) {
                    et_gps_time.text = gpsTime
                }
                et_gps_alt.text = Editable.Factory.getInstance().newEditable(entity.alt)
                val date = entity.date
                if (date != null && date.isNotEmpty()) {
                    et_date.text = date
                }
                et_exp_time.text = Editable.Factory.getInstance().newEditable(entity.expTime)
                et_flash.text = Editable.Factory.getInstance().newEditable(entity.flash)
                et_focal.text = Editable.Factory.getInstance().newEditable(entity.focal)
                et_orien.text = Editable.Factory.getInstance().newEditable(entity.orien)
                et_balance.text = Editable.Factory.getInstance().newEditable(entity.blance)
            }
        }
    }

    private fun saveTemplate() {
        val make = et_make.text.toString()
        val mode = et_mode.text.toString()
        val gpsDate = et_gps_date.text.toString()
        val time = et_gps_time.text.toString()
        val alt = et_gps_alt.text.toString()
        val date = et_date.text.toString()
        val expTime = et_exp_time.text.toString()
        val flash = et_flash.text.toString()
        val focal = et_focal.text.toString()
        val orien = et_orien.text.toString()
        val blance = et_balance.text.toString()
        val exifEntity = ExifTemplateEntity(make, mode, gpsDate, time, alt, date, expTime, flash, focal, orien, blance)
        mainKv.encode(JSON_EXIF, Gson().toJson(exifEntity))
        ToastUtils.show("保存成功！")
    }
}